package com.cc.controller;

import com.alibaba.druid.pool.DruidDataSource;
import com.cc.config.DataBaseConfiguration;
import com.cc.config.PackagePathConfiguration;
import com.cc.config.TemplateConfiguration;
import com.cc.pojo.WhereParams;
import com.cc.pojo.bo.TransBo;
import com.cc.pojo.tablestruct.NeedQueryParams;
import com.cc.service.generate.GenerateController;
import com.cc.service.generate.GenerateDao;
import com.cc.service.generate.GenerateDaoImpl;
import com.cc.service.generate.GenerateDb;
import com.cc.service.generate.GenerateDo;
import com.cc.service.generate.GenerateDto;
import com.cc.service.generate.GenerateInter;
import com.cc.service.generate.GenerateService;
import com.cc.service.generate.GenerateVo;
import com.cc.utils.beanUtil.BeanUtils;
import org.apache.velocity.app.Velocity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yaml.snakeyaml.Yaml;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;


/**
 * @User c.c
 * @Since 2023/4/19
 * @Time 10:37
 * @Description
 */
//@RequestMapping("/api")
@Controller
public class TestController {

  @Resource
  private ContextRefresher contextRefresher;
  @Autowired
  private TemplateConfiguration templateConfiguration;
  @Autowired
  private PackagePathConfiguration packagePathConfiguration;
  @Autowired
  private DataBaseConfiguration dataBaseConfiguration;


  @GetMapping("/test1")
  public void test1() {
//    System.out.println(templateConfiguration.getControllerPath());
//    System.out.println(packagePathConfiguration.toString());
//    System.err.println(dataBaseConfiguration.toString());

    TemplateConfiguration bean = BeanUtils.getBean(TemplateConfiguration.class);

    System.err.println(bean.getControllerPath());
  }

  @GetMapping("/test2")
  public void test2() {
    Properties properties = new Properties();
    properties.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    Velocity.init(properties);
//    GenerateCore generateCore = new GenerateCore();
//    generateCore.createDb();
//    generateCore.createDo();

    GenerateDb generateDb = new GenerateDb();
    GenerateDo generateDo = new GenerateDo();
    GenerateVo generateVo = new GenerateVo();
    GenerateDto generateDto = new GenerateDto();

    List<NeedQueryParams> needQueryParams = new ArrayList<>();
    needQueryParams.add(new NeedQueryParams("String", "name"));

    GenerateDao generateDao = new GenerateDao(needQueryParams);
    GenerateInter generateInter = new GenerateInter();


    List<WhereParams> whereParams = new ArrayList<>();
    whereParams.add(new WhereParams("xxx", "Condition.eq", "1"));
    whereParams.add(new WhereParams("xxx2", "Condition.like", "2"));
    whereParams.add(new WhereParams("xxx3", "Condition.contain", "3"));

    GenerateDaoImpl generateDaoimpl = new GenerateDaoImpl(needQueryParams, whereParams);
    GenerateService generateService = new GenerateService(needQueryParams);

    List<TransBo> transBos = new ArrayList<>();
//    transDtos.add(new TransDto("name",null,"姓名","String"));
//    transDtos.add(new TransDto("sex",null,"性别","String"));
//    transDtos.add(new TransDto("age",null,"年龄","String"));
//    transDtos.add(new TransDto("hight",null,"身高","String"));
//    transDtos.add(new TransDto("other",null,"其他","String"));
    GenerateController generateController = new GenerateController(needQueryParams);
  }


  @GetMapping("/changeDatabase")
  @Deprecated
  //此方法作为思路，这个项目没法实现
  public void testChangeDatabase() throws SQLException {
    DruidDataSource dataSource = BeanUtils.getBean(DruidDataSource.class);
    dataSource.restart();
    dataSource.setUrl("jdbc:mysql://localhost:3306/hexo-boot?useUnicode=yes&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=true&verifyServerCertificate=false&requireSSL=true");
    dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
    dataSource.setUsername("root");
    dataSource.setPassword("password");
    dataSource.resetStat();

    test2();

  }

  @GetMapping("/changeyaml")
  public void testchangeyaml() throws IOException, SQLException {
    InputStream applicationStream = this.getClass().getClassLoader().getResourceAsStream("application.yaml");
    Yaml yaml = new Yaml();
    Map<String, Object> yamlMap = yaml.load(applicationStream);
    System.out.println(yamlMap);

    yamlMap.put("address", "localhost:3306");
    yamlMap.put("databasename", "hexo-boot");
    yamlMap.put("password", "password");
    yamlMap.put("account", "root");

    String path = this.getClass().getClassLoader().getResource("application.yaml").getPath();

    //字符输出
    FileWriter fileWriter = new FileWriter(new File(path));
    //用yaml方法把map结构格式化为yaml文件结构
    fileWriter.write(yaml.dumpAsMap(yamlMap));
    //刷新
    fileWriter.flush();
    //关闭流
    fileWriter.close();


    DruidDataSource dataSource = BeanUtils.getBean(DruidDataSource.class);
    dataSource.restart();
    //刷新配置文件
    contextRefresher.refresh();
  }

  @RequestMapping(value = "/test", method = RequestMethod.POST)
  @ResponseBody
  public String handleSubmit(@RequestParam("input") String input, Model model) {
    String message = "You submitted: " + input;
    model.addAttribute("message", message);
    model.addAttribute("input", input);
    return "my-template";
  }
}

